package com.hubu.list;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
    static class Produce extends Thread{
        private BlockingQueue<Runnable> taskQueue=new BlockingQueue<>(20000);

        private Runnable task;

        public void execute(Runnable task){
            //当前线程还没有被打断，一直添加任务
            while(!Thread.currentThread().isInterrupted()){
                synchronized (this){
                    taskQueue.offer(task);
                }
            }
        }
    }
    //任务队列，用于存储一个个的任务
    static class BlockingQueue<T>{
        //最大容量
        private int maxSize;
        private Queue<T> taskQueue=new LinkedList<>();
        public BlockingQueue(int maxSize){
            this.maxSize=maxSize;
        }
        public void offer(T task){
            while(taskQueue.size()>=maxSize){
                synchronized (taskQueue){
                    try {
                        taskQueue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            taskQueue.offer(task);
            //通知生产者
            synchronized (taskQueue){
                taskQueue.notifyAll();
            }
        }

        public T poll(){
            while(taskQueue.isEmpty()){
                synchronized (taskQueue){
                    try {
                        taskQueue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            T task = taskQueue.poll();
            synchronized (taskQueue){
                taskQueue.notifyAll();
            }
            return task;
        }
    }
}
